package canglang.monitor.cases.action;

import canglang.monitor.cases.model.CaseTwoModel;
import canglang.monitor.cases.service.CaseTwoMgr;
import canglang.monitor.cases.to.SearchTwoTO;
import canglang.monitor.core.base.action.BaseAction;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.properties.AppDefaultProperty;
import canglang.monitor.core.base.util.DateUtil;
import canglang.monitor.core.base.util.FileUtil;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.user.model.UserModel;
import com.opensymphony.xwork2.util.CreateIfNull;
import com.opensymphony.xwork2.util.Element;

import java.io.OutputStream;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class CaseTwoAction extends BaseAction {
    private static final long serialVersionUID = -7513316734089223204L;

    private CaseTwoModel caseTwoModel;

    private CaseTwoMgr caseTwoMgr;

    private PaginatedListHelper helper;

    private SearchTwoTO searchTO;

    public CaseTwoModel getCaseTwoModel() {
        return caseTwoModel;
    }

    public void setCaseTwoModel(CaseTwoModel caseTwoModel) {
        this.caseTwoModel = caseTwoModel;
    }

    public CaseTwoMgr getCaseTwoMgr() {
        return caseTwoMgr;
    }

    public void setCaseTwoMgr(CaseTwoMgr caseTwoMgr) {
        this.caseTwoMgr = caseTwoMgr;
    }

    public PaginatedListHelper getHelper() {
        return helper;
    }

    public void setHelper(PaginatedListHelper helper) {
        this.helper = helper;
    }

    public SearchTwoTO getSearchTO() {
        return searchTO;
    }

    public void setSearchTO(SearchTwoTO searchTO) {
        this.searchTO = searchTO;
    }

    @Element(value = CaseTwoModel.class)
    @CreateIfNull(value = true)
    public List getResultList() {
        return helper != null ? helper.getList() : null;
    }

    public void setResultList(List alist) {
        if (helper == null)
            helper = new PaginatedListHelper();
        helper.setList(alist);
    }

    public String search() {
        if (searchTO == null) {
            searchTO = new SearchTwoTO();
        }
        UserModel user=(UserModel)getRequest().getSession().getAttribute("user");
        if(!user.getUserRole().equalsIgnoreCase("A")){
            searchTO.setCreateId(String.valueOf(user.getDeptId()));
        }
        if (helper == null) {
            helper = new PaginatedListHelper();
            helper.setSortCriterion("CREATE_DATE");
        }
        helper = caseTwoMgr.search(helper, searchTO);
        return SUCCESS;
    }

    public String delete() {
        for (int i = 0; i < helper.getList().size(); i++) {
            CaseTwoModel u = (CaseTwoModel) helper.getList().get(i);
            if (u != null && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("") && !StringUtil.convertEmpty2Null(u.getSelectId()).equalsIgnoreCase("false")) {
                caseTwoMgr.deleteCaseTwo(Integer.parseInt(u.getSelectId()));
            }
        }
        this.saveMessage(AppDefaultProperty.getProperty("label.delete.successful"));
        return search();
    }

    public String add() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseTwoModel.setCreateId(String.valueOf(user.getDeptId()));
        caseTwoModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseTwoModel.setCreateDt(date);
        caseTwoModel.setUpdateDt(date);
        caseTwoModel.setUpload1Cnt(FileUtil.convertToByte(caseTwoModel.getUpload1()));
        caseTwoModel.setUpload2Cnt(FileUtil.convertToByte(caseTwoModel.getUpload2()));
        caseTwoModel=caseTwoMgr.addCaseTwo(caseTwoModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.add.successful"));
        return SUCCESS;
    }

    public String view() {
        String selectId = caseTwoModel.getSelectId();
        caseTwoModel= caseTwoMgr.getCaseTwo(Integer.parseInt(selectId));
        return SUCCESS;
    }

    public String save() {
        UserModel user = (UserModel) this.getSession().getAttribute("user");
        caseTwoModel.setUpdateId(String.valueOf(user.getDeptId()));
        Date date = DateUtil.getDate();
        caseTwoModel.setUpdateDt(date);
        if (StringUtil.isNullOrBlank(caseTwoModel.getUpload1FileName())) {
            CaseTwoModel c = caseTwoMgr.getCaseTwo(caseTwoModel.getCaseTwoId());
            caseTwoModel.setUpload1FileName(c.getUpload1FileName());
            caseTwoModel.setUpload1Cnt(c.getUpload1Cnt());
        } else {
            caseTwoModel.setUpload1Cnt(FileUtil.convertToByte(caseTwoModel.getUpload1()));
        }
        if (StringUtil.isNullOrBlank(caseTwoModel.getUpload2FileName())) {
            CaseTwoModel c = caseTwoMgr.getCaseTwo(caseTwoModel.getCaseTwoId());
            caseTwoModel.setUpload2FileName(c.getUpload2FileName());
            caseTwoModel.setUpload2Cnt(c.getUpload2Cnt());
        } else {
            caseTwoModel.setUpload2Cnt(FileUtil.convertToByte(caseTwoModel.getUpload2()));
        }
        caseTwoModel=caseTwoMgr.saveCaseTwo(caseTwoModel);
        this.saveMessage(AppDefaultProperty.getProperty("label.record.modify.successful"));
        return SUCCESS;
    }

    public String download() throws Exception {
        try {
            String selectId = caseTwoModel.getSelectId();
            String recordType = caseTwoModel.getSelectType();
            caseTwoModel = caseTwoMgr.getCaseTwo(Integer.parseInt(selectId));
            getResponse().reset();
            if ("1".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseTwoModel.getUpload1FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseTwoModel.getUpload1Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }else if ("2".equalsIgnoreCase(recordType)) {
                getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + new String(caseTwoModel.getUpload2FileName().getBytes("gb2312"), "ISO8859-1"));
                getResponse().setContentType(DOWNLOAD_CONTENT_TYPE);
                byte[] content = caseTwoModel.getUpload2Cnt();
                getResponse().setContentLength(content.length);
                OutputStream out = getResponse().getOutputStream();
                out.write(content);
                out.flush();
            }
        } catch (Exception e) {
            System.out.println(e);
            throw e;
        }
        return NONE;
    }

}
